package com.jservice.listener;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jservice.framework.message.Message;
import com.jservice.framework.message.MessageListener;
import com.jservice.framework.message.annotation.Listener;
import com.jservice.framework.utils.XingeUtils;
import com.jservice.model.SmkAppUser;
import com.jservice.model.SmkSms;

/**
 * SmsListener
 *
 * @Author cwledit
 * @DATE 2017/7/4.
 */
@Listener(action = {SmkSms.ACTION_ADD})
public class SmsListener implements MessageListener {

    private static final Log LOG = Log.getLog(SmsListener.class);
    @Override
    public void onMessage(Message message) {
        SmkSms smkSms = message.getData();
        String account = smkSms.getAccount();
        //判断用户是否登录,未登录
        SmkAppUser smkAppUser = SmkAppUser.dao.doFindFirst( "id_card = ?",account);
        if (smkAppUser != null){
            String content = smkSms.getMessage();
            String id = smkSms.getId();
            String sql = "select count(*) from "+SmkSms.TABLE_NAME + " where account = ? and status = 0";
            long count = Db.queryLong(sql, smkSms.getAccount());
            int badge = new Long(count).intValue();
            String platForm = smkAppUser.getPlatform();
            if ("IOS".equalsIgnoreCase(platForm)){
                XingeUtils.iosPushNotice(content,smkAppUser.getToken(),badge,id);
            }else if ("ANDROID".equalsIgnoreCase(platForm)){
                XingeUtils.androidPushNotice(content,smkAppUser.getToken(),badge,id);
            }
        }
    }
}
